﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace DataAccess.Tools
{
    public static class XMLParser
    {
        public static string ObjectToXml<T>(T objectToSerialize)
        {
            try
            {
                XmlSerializer xmlSer = new XmlSerializer(objectToSerialize.GetType());
                StringWriter sWriter = new StringWriter();
                xmlSer.Serialize(sWriter, objectToSerialize);
                return sWriter.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public static object XmlToObject(string xmlToDeserialize, Type type)
        {
            try
            {
                XmlSerializer xmlSer = new XmlSerializer(type);
                StringReader sReader = new StringReader(xmlToDeserialize);
                object retDTO = (object)xmlSer.Deserialize(sReader);
                return retDTO;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }
    }
}
